<?php

namespace App\Http\Controllers\v1;

use App\Model\AuditModel;
use App\Model\UserModel;
use App\User;
use Cache;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class AuditController extends BaseController
{
    /**
     * 读取被举报的贴
     * @param Request $request
     * @return AuditController
     */
  public function getAuditCard(Request $request){
      $token = $request ->header('token');
      $uid = Cache::get($token)['uuid'];

//      查询用户权限
      $user = UserModel::find($uid);
      if(is_null($user)){
          return $this->error('没有这个用户！');
      }
      if($user -> permission > 1){

          $type =   $request ->get('type');//分类
          if(is_null($type)){
              return $this->error('缺少参数！');
          }
          $res  = ( new  AuditModel()) ->getAuditCard($type);

          if($res['code']){
              return $this -> success('成功！',$res['data']);
          }else{
              return $this -> error('失败！',$res['data']);
          }

      }else{
          return $this->error('没有权限！');
      }



  }


    /**
     * 删除被举报的贴
     * @param Request $request
     * @return AuditController
     */
      public function DeletedAuditCard(Request $request){
          $aid = $request ->get('aid');//audit 表id
          $cid= $request ->get('cid');//card 表id

          if(empty($aid) || empty($cid)){
              return $this->error('缺少参数！');
          }

          $res  = ( new  AuditModel()) ->DeletedAuditCard($aid,$cid);

          if($res['code']){
              return $this -> success('成功！',$res['data']);
          }else{
              return $this -> error('失败！',$res['data']);
          }


      }

    /**
     * 正常操作
     * @param Request $request
     * @return AuditController
     */
      public function AuditCard(Request $request){
          $aid = $request ->get('aid');//audit 表id

          if(empty($aid) ){
              return $this->error('缺少参数！');
          }

          $res  = ( new  AuditModel()) ->AuditCard($aid);

          if($res['code']){
              return $this -> success('成功！',$res['data']);
          }else{
              return $this -> error('失败！',$res['data']);
          }
      }

    /**
     * 举报帖子
     * @param Request $request
     * @return AuditController
     */
      public function reportCard(Request $request){
         $type = $request ->get('type');
         $cid = $request ->get('cid');
          $token = $request ->header('token');
          $uid = Cache::get($token)['uuid'];

          if(is_null($type)  || empty($cid)){
                return $this->error('缺少参数');
          }

          $res  = ( new  AuditModel()) ->reportCard($type,$cid,$uid);

          if($res['code']){
              return $this -> success('成功！',$res['data']);
          }else{
              return $this -> error('失败！',$res['data']);
          }

      }

    /**
     * 鉴权(检查用户是否有管理员权限)
     * @param Request $request
     * @return AuditController
     */
      public function AAA(Request $request){
          $token = $request ->header('token');
          $uid = Cache::get($token)['uuid'];


//          查询用户是否有权限
          $user = UserModel::find($uid);
          if( $user -> permission > 1 ){
            return $this->success('成功！');
          }else{
              return $this->error('权限不够');
          }



      }


}
